﻿using AutoMapper;
using ERPSystem.Common.Domains.supplier;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using ERPSystem.Common.DTO;
using ERPSystem.Enum.CommonErrorCodes;

namespace BasicInformationWrite.API.BasicInformationCommons.suppliers
{
    public class PurchasedGoodsCreateHandler:IRequestHandler<PurchasedGoodsCreateCommomand, Result>
    {
        private readonly IBaseRepository<PurchasedGoods> _purchasedGoodsRe;
        private readonly IMapper mapper;

        public PurchasedGoodsCreateHandler(IBaseRepository<PurchasedGoods> purchasedGoodsRe, IMapper mapper)
        {
            _purchasedGoodsRe = purchasedGoodsRe;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(PurchasedGoodsCreateCommomand request, CancellationToken cancellationToken)
        {

            var result = new Result() { Code = (int)CommonErrorCode.Added };

            try
            {
                var purchasedGoods = mapper.Map<PurchasedGoods>(request);
                purchasedGoods.CreatedOwnerId = "张三";
                purchasedGoods.CreatedTime = DateTime.Now;
                var list = await _purchasedGoodsRe.Add(purchasedGoods);
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
